package com.jzo2o.mall.cart.model.dto;

import com.jzo2o.mall.cart.model.enums.CartTypeEnum;
import com.jzo2o.mall.cart.model.enums.DeliveryMethodEnum;
import com.jzo2o.mall.common.utils.CurrencyUtil;
import com.jzo2o.mall.product.model.domain.GoodsSku;
import com.jzo2o.mall.promotion.service.PromotionTools;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Map;

/**
 * 购物车中的产品
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class CartSkuDTO extends CartBaseDTO implements Serializable {


    private static final long serialVersionUID = -894598033321906974L;


    private String sn;
    /**
     * 对应的sku DO
     */
    private GoodsSku goodsSku;

//    /**
//     * 分销描述
//     */
//    private DistributionGoods distributionGoods;

    @ApiModelProperty(value = "购买数量")
    private Integer num;

    //购买时的成交价,如果是促销商品，则使用促销价格，否则是默认价格
    @ApiModelProperty(value = "购买时的成交价")
    private Double purchasePrice;

    @ApiModelProperty(value = "小计，成交价乘以数量")
    private Double subTotal;

//    @ApiModelProperty(value = "小计")
//    private Double utilPrice;
    /**
     * 是否选中，要去结算 0:未选中 1:已选中，默认
     */
    @ApiModelProperty(value = "是否选中，要去结算")
    private Boolean checked;

    @ApiModelProperty(value = "是否免运费")
    private Boolean isFreeFreight;

    @ApiModelProperty(value = "是否失效 ")
    private Boolean invalid;

    @ApiModelProperty(value = "购物车商品错误消息")
    private String errorMessage;

    @ApiModelProperty(value = "是否可配送")
    private Boolean isShip;


    @ApiModelProperty("商品促销活动集合，key 为 促销活动类型，value 为 促销活动实体信息 ")
    private Map<String, Object> promotionMap;

    /**
     * @see CartTypeEnum
     */
    @ApiModelProperty(value = "购物车类型")
    private CartTypeEnum cartType;

    /**
     * @see DeliveryMethodEnum
     */
    @ApiModelProperty(value = "配送方式")
    private String deliveryMethod;

    /**
     * 在构造器里初始化促销列表，规格列表
     */
    public CartSkuDTO(GoodsSku goodsSku) {
        this.goodsSku = goodsSku;
        if (this.goodsSku.getUpdateTime() == null) {
            this.goodsSku.setUpdateTime(goodsSku.getCreateTime());
        }
        this.checked = true;
        this.invalid = false;
        //默认时间为0，让系统为此商品更新缓存
        this.errorMessage = "";
        this.isShip = true;
        //成交价，如果是促销商品，则使用促销价格，否则是默认价格
        this.purchasePrice = goodsSku.getPromotionFlag() != null && goodsSku.getPromotionFlag() ? goodsSku.getPromotionPrice() : goodsSku.getPrice();
        //默认为不免运费
        this.isFreeFreight = false;
//        this.utilPrice = goodsSku.getPromotionFlag() != null && goodsSku.getPromotionFlag() ? goodsSku.getPromotionPrice() : goodsSku.getPrice();
        //计算购物车小计
//        this.subTotal = CurrencyUtil.mul(this.getPurchasePrice(), this.getNum());
        this.setStoreId(goodsSku.getStoreId());
        this.setStoreName(goodsSku.getStoreName());
    }

    /**
     * 在构造器里初始化促销列表，规格列表
     */
//    public CartSkuDTO(GoodsSku goodsSku, Map<String, Object> promotionMap) {
//        this(goodsSku);
//        if (promotionMap != null && !promotionMap.isEmpty()) {
//            this.promotionMap = promotionMap;
//        }
//    }

    public void rebuildBySku(GoodsSku goodsSku) {
        this.goodsSku = goodsSku;
        //实际价格
        this.purchasePrice = goodsSku.getPromotionFlag() != null && goodsSku.getPromotionFlag() ? goodsSku.getPromotionPrice() : goodsSku.getPrice();
        //和上边一样都是实际价格?
//        this.utilPrice = goodsSku.getPromotionFlag() != null && goodsSku.getPromotionFlag() ? goodsSku.getPromotionPrice() : goodsSku.getPrice();
        //计算购物车小计
        this.subTotal = CurrencyUtil.mul(this.getPurchasePrice(), this.getNum());
        this.setStoreId(goodsSku.getStoreId());
        this.setStoreName(goodsSku.getStoreName());
    }

    //计算subTotal
    public void rebuildSubTotal() {
        this.subTotal = CurrencyUtil.mul(this.getPurchasePrice(), this.getNum());
    }

//    public Map<String, Object> getPromotionMap() {
//        return PromotionTools.filterInvalidPromotionsMap(this.promotionMap);
//    }
//
//    public Map<String, Object> getNotFilterPromotionMap() {
//        return this.promotionMap;
//    }
}
